package com.athenaeum.dataxplore.controller;

import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.athenaeum.datastore.domain.Experience;
import com.athenaeum.datastore.domain.User;
import com.athenaeum.datastore.service.UserService;
import com.athenaeum.datastore.util.UserUtil;

@Controller
public class ExperienceController {

	private static final Logger log = LoggerFactory
			.getLogger(ExperienceController.class);
	@Autowired
	private UserService userService;

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/settings/profile/experience/save", method = RequestMethod.POST)
	public String index(Locale locale,
			@ModelAttribute("experience") Experience experience,
			BindingResult result, Model model) {
		log.info("Saving Settings - Organization - Save Page...");
		if (result.hasErrors())
			return "account/organization/index";
		User user = userService.findOne(UserUtil.getUsernameInContext());
		user.addExperience(experience);
		user = userService.save(user);
		return "redirect:/settings/profile";
	}

}
